﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CardLogistics.Web.Components;
using System.Text;

using System.Data.OleDb;
using System.IO;
using CardLogistics.Model.CardArrival;


namespace CardLogistics.Web.MainPage.CardArrival.StorageCheck
{
    public partial class ListInfo : BasePage
    {
        private static readonly CardLogistics.DAL.CardArrival.Storage_Check_CardData DAL = new DAL.CardArrival.Storage_Check_CardData();
        private int _ExcelCount; //统计Excel总条数
        /// <summary>
        /// 箱数
        /// </summary>
        public int BoxNumber 
        {
            get;
            set;
        }
        /// <summary>
        /// 盒数
        /// </summary>
        public int CardNumber
        {
            get;
            set;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            WebUserControl_ToolBar1.DeleteEvent += new EventHandler(WebUserControl_ToolBar1_DeleteEvent);
            if (!IsPostBack)
            {
                //状态 0 未提交 1 待审核 2 审核中 3 驳回 4 审核不通过 5审核通过
                BindDLL(ddlIsSubmit);
                BindDLL(ddlStorageType, "basic_basictype", "typename", "id", " and typeno=2 and  status<>1 and isdelete=0"); //入库类型
                BindDLL(ddlsupName, "basic_suppliers", "suppliersname", "suppliersid", " and STATUS=0 and ISDELETE=0");  //供应商
                BindDLL(ddlBasicType, "basic_basictype", "typename", "id", " and typeno=0 and  status<>1 and isdelete=0"); //芯片类型
                BindDLL(ddlCardType, "basic_cardtype", "cardtypename", "id", " and  status<>1 and isdelete=0"); //卡片类型

                BindRpt(LoadStrWhere);
            }
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void WebUserControl_ToolBar1_DeleteEvent(object sender, EventArgs e)
        {
            string delId = this.hdf_Id.Value;
            if (!string.IsNullOrEmpty(delId))
            {
                string[] ids = delId.TrimEnd(',').Split(',');
                int resCount = 0;
                for (int i = 0; i < ids.Length; i++)
                {
                    string resStr = DAL.Delete(ids[i]);
                    if (resStr.Split(',')[0] == "ok")
                        resCount++;
                }
                BindRpt("");
            }
        }

        /// <summary>
        /// 绑定Rpt数据
        /// </summary>
        private void BindRpt(string strWhere)
        {
            int recordCount = 0;
            int pageCount = 0;
            StringBuilder sbStr = new StringBuilder();
            sbStr.Append(" 1=1 ");
            if (txt_OrderNo.Text.Trim().Length > 0)
                sbStr.Append(" and OrderNo like '%" + txt_OrderNo.Text.Trim().ToUpper() + "%' ");//订单编号
            if (txt_CheckNo.Text.Trim().Length > 0)
                sbStr.Append(" and CheckNo like '%" + txt_CheckNo.Text.Trim().ToUpper() + "%' ");//入库单号

            if (ddlStorageType.SelectedValue != "" && ddlStorageType.SelectedValue != "0")//入库类型
                sbStr.Append(" and Storagetype ='" + ddlStorageType.SelectedValue + "'");
            if (txt_StartStorageDate.Text.Trim().Length > 0)
                sbStr.Append(" and to_char(StorageDate,'yyyy-MM-dd')>='" + txt_StartStorageDate.Text.Trim() + "'");//入库开始日期
            if (txt_EndStorageDate.Text.Trim().Length > 0)
                sbStr.Append(" and to_char(StorageDate,'yyyy-MM-dd')<='" + txt_EndStorageDate.Text.Trim() + "'");//入库结束日期

            if (this.ddlsupName.SelectedItem!=null && ddlsupName.SelectedIndex != 0)
                sbStr.Append(" and SuppId='" + ddlsupName.SelectedValue.ToString() + "'");//供应商
            if (this.ddlCardType.SelectedItem!=null && ddlCardType.SelectedIndex != 0)
                sbStr.Append(" and cardtypeid='" + ddlCardType.SelectedValue.ToString() + "'");//卡类型
            if (this.ddlBasicType.SelectedItem!=null && ddlBasicType.SelectedIndex != 0)
                sbStr.Append(" and basictypeid='" + ddlBasicType.SelectedValue.ToString() + "'");//芯片类型
              //审核状态
            if (ddlIsSubmit.SelectedValue != "-1")
            {
                sbStr.Append(" and IsSubmit=" + ddlIsSubmit.SelectedValue);
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                sbStr.Append(strWhere);
            }
            DataTable dt = CardLogistics.DAL.CommonData.QuickPage_Oracle("v_Storage_Check_Card", "ID", " issubmit,storagedate desc,checkno desc", AspNetPager1.PageSize, WebPageIndex, sbStr.ToString(), "*", out pageCount, out recordCount);
            SetNetPagerHtml(AspNetPager1, dt, rptList, recordCount);
            #region 导出数据
            //导出语句
            string strExpSql = "select STORAGEDATE 入库日期,CheckNo 入库单号,storageTypeName 入库类型, CARDTYPENAME 卡片类型,TYPENAME 芯片类型,SUPPLIERSNAME 供应商,OrderNo 订单编号,boxnocount 箱数,cardnocount 盒数,EXAMINER 卡片验收员,REMARK 备注 from v_Storage_Check_Card where " + sbStr.ToString() + " order by checkno desc";
            if (!Page.IsClientScriptBlockRegistered("regExportSql"))
                Page.RegisterClientScriptBlock("regExportSql", "<script language=\"Javascript\">var qsql=\"" + Server.UrlEncode(strExpSql) + "\";</script>");
            #endregion
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            WebPageIndex = AspNetPager1.CurrentPageIndex;
            BindRpt("");
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Search_Click(object sender, EventArgs e)
        {
            WebPageIndex = 1;
            BindRpt("");
        }
    }
}